<template>
  <div class="contents">
    <div class="querys">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入" clearable size="small"
                    style="width: 240px"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="queryParams.auditStatus" clearable placeholder="请选择">
              <el-option label="全部 " value="0" />
              <el-option label="待审核 " value="1" />
              <el-option label="已通过 " value="2" />
              <el-option label="已驳回 " value="3" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button  type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询
          </el-button>
          <el-button  icon="el-icon-refresh" class="border-Blue" size="mini" @click="resetQuery">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="opt_header">
      <span>已勾选了{{selectedList.length}}条数据</span>
      <el-button :disabled="batchDisabled" @click="BatchPass" type="primary">批量通过</el-button>
    </div>
    <el-table
        :data="tableData"
        style="width: 100%">
      <el-table-column  width="50" >
        <template slot="header" slot-scope="scope">
          <el-checkbox  :indeterminate="isIndeterminate"
                        v-model="checkAllType"
                        @change="handleCheckAllChange">
          </el-checkbox>
        </template>
        <template slot-scope="scope">
          <el-checkbox  v-model="scope.row.checked"  :disabled="scope.row.auditStatus != 1"
                        @change="handleCheckedItemsChange">
          </el-checkbox>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="" :show-overflow-tooltip="true"/>
      <el-table-column prop="account" label="用户编码" width=""/>
      <el-table-column prop="phone" label="用户手机号">
        <template slot-scope="scope">
          {{maskPhoneNumber(scope.row.phone)}}
        </template>
      </el-table-column>
      <el-table-column prop="enterpriseName" label="所属组织"/>
      <el-table-column prop="createUserName" label="提交人"/>
      <el-table-column prop="createTime" label="申请时间">
        <template slot-scope="scope">
          <span> {{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="auditStatus" label="审批状态">
        <template slot-scope="scope">
          <el-tag :type="calAuditType(scope.row)">
            {{ calAudit(scope.row) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="rejectReason" label="驳回原因" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" width="160" align="center">
        <template slot-scope="scope">
          <div class="opt_table">
            <template v-if="scope.row.auditStatus ===1">
              <el-popconfirm
                  icon-color="red"
                  title="确认审批通过吗？"
                  @confirm="confirm(scope.row)"
              >
                <el-button type="text" slot="reference">通过</el-button>
              </el-popconfirm>
              <el-button style="margin-left:10px;" type="text" @click="handleReject(scope.row)">驳回</el-button>
            </template>
            <el-button type="text" @click="handleShow(scope.row)">查看</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div class="opt_page" v-show="queryParams.total>0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="queryParams.pageNo"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="queryParams.total">
      </el-pagination>
    </div>
    <rejectAudit @getList="getList" ref="rejectAudit" />
    <showUser ref="showUser" />
  </div>
</template>
<script>
import {parseTime} from '@/utils/ruoyi'
import rejectAudit from '@/views/organization/audit/personel/rejectAudit.vue'
import showUser from '@/views/organization/audit/personel/showUser.vue'
import {batchCheckRelative, checkRelative,findRelativeUserAuditList} from "@/api/organizationOuter/audit";
import {maskEmail, maskPhoneNumber} from "@/utils/fengwenxin_utils";

export default {
  components: {
    rejectAudit,
    showUser,
  },
  data(){
    return {
      checkAllType:false,
      isIndeterminate:true,
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        total: 0,
        name:'', // 企业名称
        auditStatus:'0', //  1：待审核 2：已通过 3：已驳回
      },
      activeName: 'first',
      tableData: [],
    }
  },
  computed:{
    batchDisabled(){
      return this.selectedList.length === 0;
    },
    selectedList(){
      return this.tableData.filter(item=>item.checked).map(item=>item.id)
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleCheckedItemsChange(){
      const isEqual = this.selectedList.length == this.tableData.length;;
      this.checkAllType = isEqual
      this.isIndeterminate = !isEqual;
    },
    handleCheckAllChange(val){
      this.tableData.forEach(item=>{
        const iswait = item.auditStatus === 1; // 待审核
        if(iswait){
          item.checked = val;
        }
      })
      this.handleCheckedItemsChange()
    },
    maskPhoneNumber(val){
      return maskPhoneNumber(val)
    },
    maskEmail(val){
      return maskEmail(val)
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
        name: '',
        auditStatus:'0',
      }
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.queryParams.pageNo = 1;
      this.queryParams.pageSize = val;
      this.getList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.queryParams.pageNo = val;
      this.getList()
    },
    clearParams(obj){
      const copyObj = Object.assign({},obj)
      return copyObj
    },
    initAuditList() {
      const {pageSize,pageNo,name,auditStatus} = this.queryParams;
      const resultParams = this.clearParams({
        pageSize,
        pageNo,
        name,
        auditStatus
      });
      findRelativeUserAuditList(resultParams).then(res => {
        console.log('res', res)
        const list = res.data.list;
        list.forEach(item=>{
          item.checked = false;
        })
        this.tableData = list;
        this.queryParams.total = res.data.total;
      }).catch(err => {
        console.log('err', err)
      })
    },
    parseTime(val) {
      return parseTime(val)
    },
    getList() {
        this.initAuditList()
    },
    handleReject(row) {
      this.$refs.rejectAudit.show(row)
    },
    handleShow(row) {
      this.$refs.showUser.show(row)
    },
    BatchPass() {
      const ids = this.selectedList;
      batchCheckRelative({
        ids,
        "type": "2",
        "auditStatus": "2",
      }).then((response) => {
        this.$message({
          message: '批量通过成功',
          type: 'success'
        });
        this.getList()
      }).catch(err => {
        console.error('err', err)
      });
    },
    confirm(row) {
      const id = row.id;
      checkRelative({
        id,
        "type": "2",
        "auditStatus": "2",
      }).then((response) => {
        this.$message({
          message: '成功通过',
          type: 'success'
        });
        this.getList()
      }).catch(err => {
        console.error('err', err)
      });
    },
    calAudit(row) {
      const obj = {
        '1':'待审核',
        '2':'已通过',
        '3':'已驳回',
      };
      const auditStatus = row.auditStatus ;
      return obj[auditStatus] || '-'
    },
    calAuditType(row){
      const obj = {
        '1':'',
        '2':'success',
        '3':'danger',
      };
      const auditStatus = row.auditStatus ;
      return obj[auditStatus] || ''
    }
  }
}
</script>
<style scoped lang="less">
.opt_page {
  padding-top: 20px;
}
.opt_header {
  padding-bottom: 10px;
  span{
    margin-right:10px;
  }
}
.opt_table{
  display: flex;
  justify-content: center;
}
</style>
